{% extends "manage/layout.html" %}

{% set page_title = '账号设置' %}

{% block content %}
<div class="m-content">
	<form class="form-horizontal" method="POST">
	{{ form.hidden_tag() }}
    {% if current_user.has_permission('CHANGE_VERSION'): %}
	<fieldset class="fieldset">
		<legend>系统版本</legend>
		<div style="margin-left:220px;" class="form-group">
			<select class="selectpicker form-control" id="sys_version">
				<option selected="selected" value="{{ current_version }}">{{ current_version }}（系统当前版本）</option>
				{% for version in versions %}
				<option value="{{ version }}">{{ version }}</option>
				{% endfor%}
			</select>
			<br />
			<input id="force_update" type="checkbox" value="force_update" {% if force_update %} checked="checked" {% endif %}/>
			<label for="force_update">客户端强制更新</label>
			<br />
			<label for="force_update_at">客户端强制更新日期</label>
			<input id="force_update_at" type='text'
			 {% if force_update_at %} value="{{force_update_at}}" {% endif %}/>
		</div>
		<button style="margin-left:220px;" class="btn btn-default" id="change_version">修改版本</button>
	</fieldset>
	{% endif %}
	<fieldset class="fieldset">
		<legend>账号信息</legend>
		{% from "common/_formhelper.html" import render_field %}
		{{ render_field(form.old_pwd, class_="form-control", required=True) }}

		{{ render_field(form.new_pwd, class_="form-control", required=True) }}

		{{ render_field(form.confirm_pwd, class_="form-control", required=True) }}

		<div class="col-sm-offset-2">
			<button class="btn btn-default">确认修改</button>
		</div>
	</fieldset>
	</form>
	<table data-toggle="table" class="table table-striped table-condensed table-bordered">
		<thead>
			<tr>
				<th data-sortable="true">序号</th>
				<th>机构名称</th>
				<th>机构代码</th>
				<th>下载日志</th>
				<th data-sortable="true">上传时间</th>
			</tr>
		</thead>
		<tbody>
			{% for log in log_files %}
			<tr id="{{ log[1].school_code }}">
				<td>{{ loop.index }}</td>
				<td>
					{{ log[1].school_name }}<br />
				</td>
				<td>{{ log[1].school_code }}</td>
				<td><a href="{{ url_for('manage.download_log', school_code=None, log_name=log[1].name) }}">{{ log[1].name }}</a></td>
				<td>{{ log[1].upload_time }}</td>
			</tr>
			{% endfor %}
		</tbody>
	</table>
</div>
<div class="hidden" id="current_version">{{ current_version }}</div>
{% endblock %}

{% block js %}
{% if current_user.has_permission('CHANGE_VERSION'): %}
<script>
$(function(){
	$('#change_version').bind('click', function(e){
		e.preventDefault();
		var version = $('#sys_version option:selected').val();
		var current_version = $('#current_version').text();
		var force_update = $('#force_update').is(':checked') ? '1' : '0';
		var force_update_at = $('#force_update_at').val();
		var form = {'version': version, 'force_update': force_update,
		'force_update_at': force_update_at};
		// if (version != current_version || $('#force_update').is(':checked') != Boolean($('#force_update').attr('checked'))){
		$.post('/manage/system_version', form, function(data){
			if (data['code'] != 0){
				alert('更新版本失败');
			} else {
				alert('更新版本成功');
			}
			location.reload();
		}, 'json');
		// }
	});
	$('#force_update_at').datetimepicker({
		format:'yyyy-mm-dd',
		autoclose:true,
		todayBtn:true,
		minView:2,
		language: 'zh-CN'
	});
});
</script>
{% endif %}
{% endblock %}
